<?php
/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_parent_user_relation extends parent_user_relation {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }
    
    public function __construct() {
        $this->_table   = 'parent_user_relation';
        $this->_pk      = 'pur_id';
        $this->_foreign_key = 'pur_s_user_id';
        $this->_keyword = '';
        parent::__construct();
    }
    /*
     * 	取出一条信息
     */
    public function do_detail($id = '') {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $detail = array();
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        $detail = $this->one_info($this->_table, $this->_pk, $id);
        $detail['user_realname'] = $this->get_field_value_by_id('user_detail', 'user_id', $detail['pur_s_user_id'], 'user_realname');
        //DEBUG 输出
        $return['code']="0";
        $return['data']=$detail;
        if($_G['api']){
            //DEBUG 接口输出
            echo format_data($return, $_G['api'], $_G['mod'], $_G['action'], $_G['do'],'pc','msg_admin','admin');
        }else{
            //DEBUG 模板输出
            include template($_G['mod'].'/'.$_G['action'].'/'.$_G['do']);
        }
    }

    /*
     *  添加系统信息
    */
    public function do_add() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $return['code'] = '1';
        $request_data = isset($_REQUEST[$this->_table]) && !empty($_REQUEST[$this->_table]) ? $_REQUEST[$this->_table] : '';
        if ($request_data) {
            $data=$request_data;//TODO 和数据库字段数组求差集 以便过滤冗余数据 避免SQL插入不匹配问题
            //$data['modify_dateline']=TIMESTAMP;
            $insert_id = $this->add($this->_table, $data);
            if ($insert_id) {
                $return['code'] = '0';
                $return['data']['id'] = (string)$insert_id;
            }
            if($_G['api']){
                echo format_data($return, $_G['api'], $_G['mod'], $_G['action'], $_G['do'],'pc','msg_admin','admin');die;
            }else{
                showmessage('操作成功','index.php?mod='.$_G['mod'].'&action='.$_G['action'].'&do=index');
            }
        }else{
            include template($_G['mod'].'/'.$_G['action'].'/'.$_G['do']);
        }
    }

    /*
     *  编辑系统信息
     */

    public function do_edit() {
        global $_G;
        ini_set("display_errors","On");
        error_reporting(E_ALL);
        $return['code'] = '1';
        $request_data = isset($_REQUEST[$this->_table]) && !empty($_REQUEST[$this->_table]) ? $_REQUEST[$this->_table] : '';
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        if ($request_data && $id) {
            $data=$request_data;//TODO 和数据库字段数组求差集 以便过滤冗余数据 避免SQL插入不匹配问题
            //$data['modify_dateline']=TIMESTAMP;
            $where = array($this->_pk => $id);
            $effect_row = $this->edit($this->_table, $data, $where);
            if ($effect_row) {
                $return['code'] = '0';
            }
            if($_G['api']){
                echo format_data($return, $_G['api'], $_G['mod'], $_G['action'], $_G['do'],'pc','msg_admin','admin');die;
            }else{
                showmessage('操作成功','index.php?mod='.$_G['mod'].'&action='.$_G['action'].'&do=index');
            }
        }else{
            include template($_G['mod'].'/'.$_G['action'].'/'.$_G['do']);
        }        
    }

    /*
     *  删除系统信息
    */
    public function do_delete() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        $return['code'] = '1';
        if ($id) {
            $where = array($this->_pk => $id);
            $effect_row = $this->delete($this->_table, $where, $limit = 1);
            if ($effect_row) {
                $return['code'] = '0';
            }
        }
        if($_G['api']){
            //DEBUG 接口输出
            echo format_data($return, $_G['api'], $_G['mod'], $_G['action'], $_G['do'],'pc','msg_admin','admin');
        }else{
            //DEBUG 模板输出
            showmessage('操作成功','index.php?mod='.$_G['mod'].'&action='.$_G['action'].'&do=index');
        }
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $wheresql = "";
        $foreign_key = isset($_REQUEST['foreign_key']) ? $_REQUEST['foreign_key'] : '';
        if ($foreign_key) {
            $wheresql .= " AND ".$this->_foreign_key." = '" . $foreign_key . "' ";
        }
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if ($keyword) {
            $wheresql .= " AND ".$this->_keyword." LIKE '%" . $keyword . "%' ";
        }
        $orderby = " ORDER BY ".$this->_pk." DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->index($this->_table, $page_condition);
        foreach($page_result['page_data'] AS $key => $value){
            $page_result['page_data'][$key]['user_realname'] = $this->get_field_value_by_id('user_detail', 'user_id', $value['pur_s_user_id'], 'user_realname');
        }
        if ($_G['api']) {
            $return['code'] = '0';
            $return['data'] = $page_result;
            echo format_data($return, $_G['api'], $_G['mod'], $_G['action'], $_G['do'],'pc','msg_admin','admin');
        } else {
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=".$_G['mod']."&action=".$_G['action']."&do=".$_G['do']."&keyword=".$keyword);//TODO 路由方法优化
            include template($_G['mod'].'/'.$_G['action'].'/'.$_G['do']);
        }
    }
}

?>